/*
 * $Id: $
 * 
 * Copyright (c) 2001-2008 Accentrix Company Limited. All Rights Reserved.
 * 
 * Accentrix Company Limited. ("Accentrix") retains copyright on all text, source
 * and binary code contained in this software and documentation. Accentrix grants
 * Licensee a limited license to use this software, provided that this copyright
 * notice and license appear on all copies of the software. The software source
 * code is provided for reference purposes only and may not be copied, modified 
 * or distributed.
 * 
 * THIS SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS," WITHOUT ANY WARRANTY OF
 * ANY KIND UNLESS A SEPARATE WARRANTIES IS PURCHASED FROM ACCENTRIX AND REMAINS
 * VALID.  ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ACCENTRIX SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING OR MODIFYING THE
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 * IN NO EVENT WILL ACCENTRIX BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
 * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE
 * USE OF OR INABILITY TO USE SOFTWARE, EVEN IF ACCENTRIX HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */
package com.lightsaber.trade.cloth.web.programme;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.lightsaber.trade.core.web.BaseAction;
import com.lightsaber.trade.cloth.entity.programme.Programme;
import com.lightsaber.trade.cloth.entity.programme.ProgrammeCode;
import com.lightsaber.trade.cloth.entity.programme.SubjectGroup;
import com.lightsaber.trade.cloth.service.programme.ProgrammeCodeManager;
import com.lightsaber.trade.cloth.service.programme.ProgrammeManager;
import com.lightsaber.trade.cloth.service.programme.ProgrammePlanProjectionTypeManager;
import com.lightsaber.trade.cloth.service.programme.SubjectGroupManager;

public class ProgrammeSubjectGroupAction extends BaseAction implements SessionAware {

    private static final long serialVersionUID = 4118497987743384976L;

    protected Map<String, Object> session;

    @Override
    public void setSession(final Map<String, Object> session) {
        this.session = session;
    }

    public static final String SELECTED_CODE_MAP = "SELECTED_CODE_MAP";

    @Autowired
    private ProgrammeManager programmeManager;
    @Autowired
    ProgrammeCodeManager programmeCodeManager;
    @Autowired
    SubjectGroupManager subjectGroupManager;

    @Autowired
    ProgrammePlanProjectionTypeManager programmePlanProjectionTypeManager;

    private List<SubjectGroup> subjectGroups;

    private String[] groupIds;

    // --------------input page---------------START
    private Page<ProgrammeCode> page;
    private SubjectGroup subjectGroup;
    private String code;
    private String programmeId;
    private String[] receiverCodes;
    private String searchCode;

    private boolean hasStream;

    // --------------input page---------------END

    public String delete() throws Exception {
        if (groupIds != null && groupIds.length > 0) {
            for (String groupId : groupIds) {

                SubjectGroup willDelGroup = subjectGroupManager.get(groupId);

                // Heal the feeder feeders and receivers
                programmePlanProjectionTypeManager.changeFeederOnDeleteGroup(willDelGroup);

                subjectGroupManager.delete(willDelGroup);
            }
        }

        return list();
    }

    @Override
    public String execute() {
        return list();
    }

    public String list() {
        // Clear codes map in session first
        session.remove(SELECTED_CODE_MAP);

        Programme programme = programmeManager.get(programmeId);
        code = programme.getCode();

        ProgrammeCode programmeCode = programmeCodeManager.getByCode(code);

        hasStream = programmeManager.hasStream(code);

        subjectGroups = programmeCode.getSubjectGroups();

        return SUCCESS;
    }

    @Override
    public String input() throws Exception {
        getSelectedCodesMap();

        if (subjectGroup != null && StringUtils.isNotBlank(subjectGroup.getId())) {
            subjectGroup = subjectGroupManager.get(subjectGroup.getId());

        }

        if (page == null) {
            page = new Page<ProgrammeCode>();
        }
        page.setPageSize(3);

        page = programmeCodeManager.findUseableCodes(code, page, searchCode);

        return INPUT;
    }

    public String save() throws Exception {
        Map<String, String> selectedCodesMap = getSelectedCodesMap();

        subjectGroupManager.save(code, subjectGroup, selectedCodesMap);

        addActionMessage(getText(PROCESSED_SUCCESS));

        return list();
    }

    public void ajaxValidateSubject() {

        boolean same = subjectGroupManager.hasSameSubjectGroup(code,
                subjectGroup.getPlanningExercise(), subjectGroup.getId());
        if(same)
            Struts2Utils.renderText("true");
        else
            Struts2Utils.renderText("false");
    }

    private Map<String, String> getSelectedCodesMap() {
        Map<String, String> selectedCodesMap = (Map<String, String>) session.get(SELECTED_CODE_MAP);

        if (selectedCodesMap == null) {
            selectedCodesMap = new HashMap<String, String>();
            session.put(SELECTED_CODE_MAP, selectedCodesMap);
        }

        if (receiverCodes != null && receiverCodes.length > 0) {
            for (String code : receiverCodes) {
                selectedCodesMap.put(code, code);
            }
        }

        return selectedCodesMap;
    }

    public String getProgrammeId() {
        return programmeId;
    }

    public void setProgrammeId(final String programmeId) {
        this.programmeId = programmeId;
    }

    public String getCode() {
        return code;
    }

    public void setCode(final String code) {
        this.code = code;
    }

    public List<SubjectGroup> getSubjectGroups() {
        return subjectGroups;
    }

    public void setSubjectGroups(final List<SubjectGroup> subjectGroups) {
        this.subjectGroups = subjectGroups;
    }

    public SubjectGroup getSubjectGroup() {
        return subjectGroup;
    }

    public void setSubjectGroup(final SubjectGroup subjectGroup) {
        this.subjectGroup = subjectGroup;
    }

    public Page<ProgrammeCode> getPage() {
        return page;
    }

    public void setPage(final Page<ProgrammeCode> page) {
        this.page = page;
    }

    public String[] getReceiverCodes() {
        return receiverCodes;
    }

    public void setReceiverCodes(final String[] receiverCodes) {
        this.receiverCodes = receiverCodes;
    }

    public String[] getGroupIds() {
        return groupIds;
    }

    public void setGroupIds(final String[] groupIds) {
        this.groupIds = groupIds;
    }

    public String getSearchCode() {
        return searchCode;
    }

    public void setSearchCode(String searchCode) {
        this.searchCode = searchCode;
    }

    public boolean isHasStream() {
        return hasStream;
    }

    public void setHasStream(boolean hasStream) {
        this.hasStream = hasStream;
    }

}
